/*
 * Copyright 2024 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.gitee.joyz.api.result.i18n;

import io.gitee.joyz.api.result.util.ObjectUtils;

import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 基于资源文件的国际化消息抽象类
 *
 * @author JoyZ
 */
public abstract class AbstractResourceLocaleMessage extends AbstractLocaleMessage {

    /**
     * 资源包缓存时间永不过期
     */
    public static final int CACHE_TIME_NEVER_EXPIRE = -1;

    private final Set<String> baseNamesSet = new LinkedHashSet<>(2);

    /**
     * ClassLoader 实例
     */
    private ClassLoader classLoader;

    /**
     * 资源包默认编码格式
     */
    private String defaultEncoding;

    /**
     * 资源包缓存时间, 单位: seconds (秒).
     * <p><b>Default:</b> -1 (永不过期) {@link #CACHE_TIME_NEVER_EXPIRE}
     */
    private int cacheTime = CACHE_TIME_NEVER_EXPIRE;

    /**
     * 设置资源包名称
     *
     * @param baseName 资源包名称
     */
    public void setBaseName(String baseName) {
        setBaseNames(baseName);
    }

    /**
     * 批量设置资源包名称.
     * 该方法会清空之前已经配置的所有资源名称
     *
     * @param baseNames 资源包名称
     */
    public void setBaseNames(String... baseNames) {
        baseNamesSet.clear();

        addBaseNames(baseNames);
    }

    /**
     * 批量添加资源包名称.
     *
     * @param baseNames 资源包名称
     */
    public void addBaseNames(String... baseNames) {
        if (!ObjectUtils.isEmpty(baseNames)) {
            baseNamesSet.addAll(Arrays.stream(baseNames)
                    .map(String::trim)
                    .filter(ObjectUtils::hasText)
                    .collect(Collectors.toSet()));
        }
    }

    /**
     * 获取 baseName 集合.
     *
     * @return 返回设置的 baseName 集合
     */
    public Set<String> getBaseNames() {
        return baseNamesSet;
    }

    /**
     * 返回设置的 ClassLoader 实例.
     *
     * @return 如果未设置则返回 {@code null}
     */
    protected ClassLoader getClassLoader() {
        return classLoader;
    }

    /**
     * 获取资源包默认编码.
     *
     * @return 如果未设置, 则返回 {@code null}
     */
    protected String getDefaultEncoding() {
        return defaultEncoding;
    }

    /**
     * 获取资源包缓存时间.
     * <p><b>Default:</b> -1 (永不过期) {@link #CACHE_TIME_NEVER_EXPIRE}
     *
     * @return 缓存时间
     */
    protected int getCacheTime() {
        return cacheTime;
    }

    public void setClassLoader(ClassLoader classLoader) {
        this.classLoader = classLoader;
    }

    public void setDefaultEncoding(String defaultEncoding) {
        this.defaultEncoding = defaultEncoding;
    }

    public void setCacheTime(int cacheTime) {
        this.cacheTime = cacheTime;
    }
}
